home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Can you printf a long
- Date: Thu, 04 Apr 96 21:41:30 GMT
- Organization: none
- Message-ID: <828654090snz@genesis.demon.co.uk>
- References: <4ju8o1$dc3@news.netam.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ju8o1$dc3@news.netam.net>
- bgc@alpha.netam.net "The Bowling Green Connection" writes:
-
- >I had some trouble with this code:
- >(I'm using gcc on Digital Unix)
- >
- >int main() {
- > long i;
- >
- > while(1) {
- > i++;
- > printf("%f\n", i);
- > }
- >
- > return 0;
- >}
- >
- >The numbers printed to the screen as 0.00000.
- >(Nevermind that this is an infinite loop--I was piping the output to
- >a file which made me run out of disk space. This is just for testing)
- >
- >But when I changed %f to %d, the numbers printed correctly.
-
- The correct format specifier for long is %ld. printf has a variable argument
- list which means the compiler doesn't know what the correct type for that
- argument is. Therefore it is up to you to match the corresponding format
- specifier and argument(s). If you get it wrong the behaviour is undefined -
- anything can happen.
-
- Both %f printing as 0.00000 and %d printing the numbers you expected are
- valid actions for undefined behaviour.
-
- >I thought that %d had the same limitations as an int..that is,
- >it could only print about 4 bits of information,
-
- The C language guarantees an int can represent at least the numbers in the
- range -32767 to 32767 which effectively puts a minimum size of 16 bits
- on it. There is no upper limit. Many systems use 32 bit ints, some 64 bits,
- a few possibly even more.
-
- >whereas a long
- >is capable of more (8 or 16 bits),
-
- A long is required to be able to represent -2147483647 to 2147483647 i.e.
- effectively a minimum of 32 bits.
-
- >so wouldn't %f (float) be able
- >to better handle those long numbers?
-
- %f is the printf format specifier for a double. Since the corresponding
- argument is part of a variable argument list any float argument is promoted
- to double so %f effectively covers both floats and doubles. %lf results
- in undefined behaviour - don't use it.
-
- Doubles simply have a different internal representation (and quite possibly
- size). It is simply interpreting the bit pattern the wrong way.
-
- %d can write any value that an int on that system can hold. Similarly %ld
- can write any value that a long can hold.
-
- > And why did %d work
- >correctly, even up to 634,000 (that's when my disk space ran out.. :))
-
- Your system probably has 32 bit ints.
-
- >Well, then I changed the "long i" declaration to "int i", and changed %f
- >to %d, and I got the SAME results! The numbers went up to 634,000!
- >I didn't think an int could handle this much!
-
- You can't rely on this portably but it certainly can be true for a
- particular implementation.
-
- >Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
- >BOTH returned a 4!!! That can't be right, can it??!
-
- Certainly and is quite common. int can't be larger than long or smaller than
- short but it can be the same size. In fact implementations exist where
- char, short, int and long are all the same size (which of course must be
- at least 32 bits). In that case sizeof will report 1 for all of those types
- since it reports in units of a char (also called a byte in C. That can be
- 8 bits or more, no upper limit).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-